package com.aquila.bible.activity;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import com.aquila.bible.MyBaseActivity;
import com.aquila.bible.R;
import com.umeng.update.UmengUpdateAgent;
import com.umeng.update.UmengUpdateListener;
import com.umeng.update.UpdateResponse;

public class LoadingActivity extends MyBaseActivity {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_loading) ;
		
		UmengUpdateAgent.setUpdateAutoPopup(false);
		UmengUpdateAgent.setUpdateOnlyWifi(false);
		UmengUpdateAgent.setUpdateListener(new UmengUpdateListener() {
			@Override
			public void onUpdateReturned(int updateStatus,
					UpdateResponse updateInfo) {
				switch (updateStatus) {
				case 0: // has update
					update(updateInfo);
					break;
				case 1: // has no update
					toMain();
					break;
				case 2: // none wifi
					update(updateInfo);
					break;
				case 3: // time out
					toMain();
					break;
				default:
					toMain();
				}
			}
		});
		UmengUpdateAgent.update(this);
	}
	
	
	
	
	 /**
     * 更新的代码控制
     * @param updateInfo
     *2012-8-20 下午4:52:31
     */
    private void update(UpdateResponse updateInfo) {
		
		String log = updateInfo.updateLog;
		final String url = updateInfo.path;
		String version = updateInfo.version;
		String message = log.split("-")[0];
		String type = log.split("-")[1];

		if ("Y".equals(type) || "N".equals(type)) {
			Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("检测到最新版本" + version);
			builder.setMessage(message);
			builder.setPositiveButton("立即更新",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							downApk(url);
							android.os.Process.killProcess(android.os.Process
									.myPid());
						}
					});
			if ("N".equals(type)) {
				builder.setNegativeButton("以后再说",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
								dialog.dismiss();
								toMain();
							}
						});
			}
			builder.show();
		} else if ("U".equals(type)) {
			downApk(url);
			android.os.Process.killProcess(android.os.Process.myPid());
		}
	}
    
    
    /**
     * 下载更新
     * @param url
     *2012-8-20 下午4:44:24
     */
	private void downApk(String url) {
		if (!"".equals(url)) {
			try {
				Uri uri = Uri.parse(url);
				Intent intent = new Intent(Intent.ACTION_VIEW, uri);
				startActivity(intent);
			} catch (Exception e) {
			}
		}
	}
    
    /**
     * 进入首页
     * 
     *2012-8-20 下午4:53:50
     */
	private void toMain() {
		Intent intent = new Intent(LoadingActivity.this, HolyBibleActivity.class);
        startActivity(intent);
        finish();
	}
    
	
	
	
}
